package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.TransportTaskExecutorRpcService;
import com.hunttown.mes.rpc.domain.TransportTaskExecutorDTO;
import com.hunttown.mes.rpc.domain.query.TransportTaskExecutorDTOQuery;
import com.hunttown.mes.service.TransportTaskExecutorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.TransportTaskExecutorConverts.convert;

/**
 * created by wangjunfu on 2020-05-20 17:34:39
 */
@Service
public class TransportTaskExecutorRpcServiceImpl implements TransportTaskExecutorRpcService {

    private final Logger logger = LoggerFactory.getLogger(TransportTaskExecutorRpcServiceImpl.class);

    private final TransportTaskExecutorService transportTaskExecutorService;

    @Autowired
    public TransportTaskExecutorRpcServiceImpl(TransportTaskExecutorService transportTaskExecutorService) {
        this.transportTaskExecutorService = transportTaskExecutorService;
    }

    @Override
    public TransportTaskExecutorDTO insert(TransportTaskExecutorDTO transportTaskExecutorDTO) {
        try {
            return convert(transportTaskExecutorService.insert(convert(transportTaskExecutorDTO)));
        } catch (Exception e) {
            logger.error("TransportTaskExecutor create failed.", e);
            throw new RuntimeException("TransportTaskExecutor create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(TransportTaskExecutorDTO transportTaskExecutorDTO) {
        try {
            return transportTaskExecutorService.updateInfoById(convert(transportTaskExecutorDTO));
        } catch (Exception e) {
            logger.error("TransportTaskExecutor update by id failed.", e);
            throw new RuntimeException("TransportTaskExecutor update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(TransportTaskExecutorDTOQuery transportTaskExecutorDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportTaskExecutorDTOQuery.getW_id() != null && transportTaskExecutorDTOQuery.getW_id() > 0) {
                query.put("w_id", transportTaskExecutorDTOQuery.getW_id());
            }

            if (transportTaskExecutorDTOQuery.getIds() != null && !transportTaskExecutorDTOQuery.getIds().equals("")) {
                query.put("ids", transportTaskExecutorDTOQuery.getIds());
            }

            if (transportTaskExecutorDTOQuery.getW_taskId() != null) {
                query.put("w_taskId", transportTaskExecutorDTOQuery.getW_taskId());
            }

            if (transportTaskExecutorDTOQuery.getW_playerType() != null) {
                query.put("w_playerType", transportTaskExecutorDTOQuery.getW_playerType());
            }

            if (transportTaskExecutorDTOQuery.getW_executor() != null) {
                query.put("w_executor", transportTaskExecutorDTOQuery.getW_executor());
            }

            if (transportTaskExecutorDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportTaskExecutorDTOQuery.getStopFlag());
            }

            if (transportTaskExecutorDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportTaskExecutorDTOQuery.getDeleteFlag());
            }

            return transportTaskExecutorService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("TransportTaskExecutor update by query failed.", e);
            throw new RuntimeException("TransportTaskExecutor update by query failed, Please order your code.", e);
        }
    }

    @Override
    public TransportTaskExecutorDTO selectById(Integer id) {
        try {
            return convert(transportTaskExecutorService.selectById(id));
        } catch (Exception e) {
            logger.error("TransportTaskExecutor select by id failed.", e);
            throw new RuntimeException("TransportTaskExecutor select by id failed, Please order your code.", e);
        }
    }

    @Override
    public TransportTaskExecutorDTO selectObjByQuery(TransportTaskExecutorDTOQuery transportTaskExecutorDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportTaskExecutorDTOQuery.getTaskId() != null) {
                query.put("taskId", transportTaskExecutorDTOQuery.getTaskId());
            }

            if (transportTaskExecutorDTOQuery.getPlayerType() != null) {
                query.put("playerType", transportTaskExecutorDTOQuery.getPlayerType());
            }

            if (transportTaskExecutorDTOQuery.getExecutor() != null) {
                query.put("executor", transportTaskExecutorDTOQuery.getExecutor());
            }

            if (transportTaskExecutorDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportTaskExecutorDTOQuery.getStopFlag());
            }

            if (transportTaskExecutorDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportTaskExecutorDTOQuery.getDeleteFlag());
            }

            return convert(transportTaskExecutorService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("TransportTaskExecutor select obj by query failed.", e);
            throw new RuntimeException("TransportTaskExecutor select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<TransportTaskExecutorDTO> getListForPage(TransportTaskExecutorDTOQuery transportTaskExecutorDTOQuery) {
        Query query = Query.page(transportTaskExecutorDTOQuery.getPn(), transportTaskExecutorDTOQuery.getPageSize());

        if (transportTaskExecutorDTOQuery.getId() != null && transportTaskExecutorDTOQuery.getId() > 0) {
            query.put("id", transportTaskExecutorDTOQuery.getId());
        }

        if (transportTaskExecutorDTOQuery.getIds() != null && !transportTaskExecutorDTOQuery.getIds().equals("")) {
            query.put("ids", transportTaskExecutorDTOQuery.getIds());
        }

        if (transportTaskExecutorDTOQuery.getTaskId() != null) {
            query.put("taskId", transportTaskExecutorDTOQuery.getTaskId());
        }

        if (transportTaskExecutorDTOQuery.getPlayerType() != null) {
            query.put("playerType", transportTaskExecutorDTOQuery.getPlayerType());
        }

        if (transportTaskExecutorDTOQuery.getExecutor() != null) {
            query.put("executor", transportTaskExecutorDTOQuery.getExecutor());
        }

        if (StringUtils.isNotBlank(transportTaskExecutorDTOQuery.getLikeName())) {
            query.put("likeName", transportTaskExecutorDTOQuery.getLikeName());
        }

        if (StringUtils.isNotBlank(transportTaskExecutorDTOQuery.getShowDate())) {
            query.put("showDate", transportTaskExecutorDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(transportTaskExecutorDTOQuery.getStartDate())) {
            query.put("startDate", transportTaskExecutorDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(transportTaskExecutorDTOQuery.getEndDate())) {
            query.put("endDate", transportTaskExecutorDTOQuery.getEndDate());
        }

        if (StringUtils.isNotBlank(transportTaskExecutorDTOQuery.getOrderBy())) {
            query.put("orderBy", transportTaskExecutorDTOQuery.getOrderBy());
        }

        if (transportTaskExecutorDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", transportTaskExecutorDTOQuery.getStopFlag());
        }

        if (transportTaskExecutorDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", transportTaskExecutorDTOQuery.getDeleteFlag());
        }

        List<TransportTaskExecutorDTO> list = convert(transportTaskExecutorService.selectByQuery(query));
        Page<TransportTaskExecutorDTO> page = Page.from(transportTaskExecutorDTOQuery.getPn(), transportTaskExecutorDTOQuery.getPageSize(), list);
        page.setTotalItems(transportTaskExecutorService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return transportTaskExecutorService.deleteById(id);
        } catch (Exception e) {
            logger.error("TransportTaskExecutor delete failed.", e);
            throw new RuntimeException("TransportTaskExecutor delete failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean deleteByIdForPhysical(Integer id) {
        try {
            return transportTaskExecutorService.deleteByIdForPhysical(id);
        } catch (Exception e) {
            logger.error("CustomerWorkExecutor physical delete failed.", e);
            throw new RuntimeException("TransportTaskExecutor physical delete failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean deleteByTaskId(Integer taskId, Integer playerType) {
        try {
            return transportTaskExecutorService.deleteByTaskId(taskId, playerType);
        } catch (Exception e) {
            logger.error("CustomerWorkExecutor delete by task id failed.", e);
            throw new RuntimeException("TransportTaskExecutor delete by task id failed, Please order your code.", e);
        }
    }
}